<?php
// $Id$

/**
 * Controller_Faculty 控制器
 */
class Controller_Faculty extends Controller_Prepare
{
    /**
     * 师资力量介绍专用页面
     * 介绍老师/查看详细/课件下载
     *
     * FacultyList
     *
     * 本函数用于根据输入条件显示教师列表
     *
     * @param int major_id CHECK(is_numeric, IN majors.id 列)
     * @param int title_id CHECK(is_numeric, IN titles.title_id 列)
     *
     * 检查参数顺序：1. isset() 2. is_type() 3. in_array()
     *
     */
    public function actionFacultyList()
    {
        // Error handling 初步过滤获得的值，必须满足值类型
        $Gtitle_id  = isset($_GET['title_id']) && is_numeric($_GET['title_id']) ? $_GET['title_id'] : NULL;
        $Gmajor_id  = isset($_GET['major_id']) && is_numeric($_GET['major_id']) ? $_GET['major_id'] : NULL;

        /**
         * 准备合法的 title_id. major_id 数组
         *
         * @var array $alltids, $allmids
         * $alltids = Title::find()->all()->query()->values('title_id');
         * $allmids = Major::find()->all()->query()->values('major_id');
         *
         * NOTICE: 为节约效率，暂时不查询，只在需要时进行查询
         */

        // 正式开始查询
        if($Gmajor_id && in_array($Gmajor_id, Major::find()->all()->query()->values('major_id')))
        {
            /**
             * 执行时 SQL
             * SELECT * FROM teachers WHERE major_id = _A_ AND status = 'in'
             * ORDER BY teacher_id ASC;
             */
            $teachers = Teacher::find('major_id = ? AND status = ?', $Gmajor_id, 'in')->all()->order('teacher_id ASC')->query();
            $css = "a[href*='_{$Gmajor_id}.html']".HIGHLIGHT_CSS;
        }
        elseif($Gtitle_id && in_array($Gtitle_id, Title::find()->all()->query()->values('title_id')))
        {
            /**
             * 执行时 SQL
             * SQL：SELECT * FROM teachers
             * JOIN teacher_title ON teachers.teacher_id = teacher_title.teacher_id
             * JOIN titles        ON titles.title_id     = teacher_title.title_id
             * WHERE titles.title_id =  _A_ AND teachers.status = 'in'
             * ORDER BY teachers.teacher_id ASC;
             */
            $tmptitles = TeacherTitle::find('title_id = ?', $Gtitle_id)->all()->query()->values('teacher_id');
            $teachers = Teacher::find('teacher_id in (?) AND status = ?', $tmptitles, 'in')->all()->order('teacher_id ASC')->query();
            $css = "a[href*='_{$Gtitle_id}.html']".HIGHLIGHT_CSS;
        }
        else
        {
            /**
             * 执行时 SQL
             * SQL：SELECT * FROM teachers WHERE status = 'in'
             * ORDER BY teacher_id ASC;
             */
            $teachers = Teacher::find('status = ?', 'in')->all()->order('teacher_id ASC')->query();
            $css = '';
        }

        $this->_view['teachers'] = $teachers;
        $this->_view['css']      = $css;
    }

    /**
     * FacultyDetail
     *
     * @param int teacher_id CHECK(is_numeric, IN teachers.teacher_id 列)
     *
     * 检查参数顺序：1. isset() 2. is_type() 3. in_array()
     *
     */
    public function actionFacultyDetail()
    {
        // Error handling 初步过滤获得的值，必须满足值类型
        $Gteacher_id = isset($_GET['teacher_id']) && is_numeric($_GET['teacher_id']) ? $_GET['teacher_id'] : NULL;

        /**
         * 检测 teacher_id 合法性
         */
        $teacher = Teacher::find('teacher_id = ?', $Gteacher_id)->query();
        if($teacher->teacher_id == '')
        {
            $Gteacher_id = NULL;
        }

        // 不合法的输入 teacher_id： 非整数，或是整数但无对应的teacher，如9998，查询结果为空
        if($Gteacher_id == NULL)
        {
            return $this->_redirect(url('faculty/facultylist'));
        }

        /**
         * CSS HIGHLIGHT
         * 一个老师可以有多个头衔， foreach( $titles as $tt ){ $css }
         *
         * @var string css
         */
        $css = '';
        $css .= ".tag_box a[href$='_系_{$teacher->major->major_id}.html'],";
        $tmpcss = '';
        foreach($teacher->titles as $tt) {
            $tmpcss .= "a[href$='_{$tt->title_id}.html']";
        }
        $css .= $tmpcss;
        $css .= HIGHLIGHT_CSS;

        $this->_view['teacher'] = $teacher;
        $this->_view['css']     = $css;
    }

    /**
     * FacultyGarden
     *
     * @param int teacher_id CHECK(is_numeric, IN teachers.teacher_id 列)
     * @param int course_id  CHECK(is_numeric, IN courses.course_id 列)
     * @param string code    NOT AS(QUERY)
     *
     * 检查参数顺序：1. isset() 2. is_type() 3. in_array()
     *
     */
    public function actionFacultyGarden()
    {
        // Error handling 初步过滤获得的值，必须满足值类型
        $Gteacher_id = isset($_GET['teacher_id']) && is_numeric($_GET['teacher_id']) ? $_GET['teacher_id'] : NULL;
        $Gcourse_id  = isset($_GET['course_id'])  && is_numeric($_GET['course_id'])  ? $_GET['course_id']  : NULL;
        $Pcode       = isset($_POST['code']) ? $_POST['code'] : NULL;

        /**
         * 检测 teacher_id 合法性
         */
        $teacher = Teacher::find('teacher_id = ?', $Gteacher_id)->query();
        // Error handling 处理故意输入 不存在于 teacher_id 列的 id，使之重定向到教师列表
        if($teacher->teacher_id == '')
        {
            $Gteacher_id = NULL;
        }

        // 不合法的输入 teacher_id： 非整数，或是整数但无对应的teacher，如9998，查询结果为空
        if($Gteacher_id == NULL)
        {
            return $this->_redirect(url('faculty/facultylist'));
        }

        // Error handling 处理故意输入 不存在于 course_id 列的 id，使之重定向到 course_id = 第一个的课程信息
        $cid = $teacher->courses->values('course_id');
        if(!empty($cid) && (!isset($Gcourse_id) || !in_array($Gcourse_id, $cid)))            // 注意，课程对应可能为空
        {
            $Gcourse_id = $cid[0];
        }

        /**
          * 获取当前课程信息
         *
         * @var object $currentCourse
         */
        $currentCourse = $teacher->courses->search('course_id', $Gcourse_id);

        /**
         * Pcode <> $teacher->accesscode
         * 判断流程：
         * 可能情况：1. 操作未输入
         *                       1.1 首次访问 操作：初始化 SESSION
         *                       1.2 旧密码已保存，同一老师，正确 flag = 1
         *                       1.3 旧密码已保存，同一老师，错误 noaccess
         *                       1.4 旧密码已保存，不同老师，正确 firstTry
         *                       1.5 旧密码已保存，不同老师，错误 firstTry
         *           2. 操作已输入
         *                       2.1 输入正确 flag = 1
         *                       2.1 输入错误 noaccess
         */
        /* 操作未输入，且过去3小时内未输入过，初始化 */
        if(!$Pcode && !isset($_SESSION['code']))
        {
            $_SESSION['code']             = 'fisrtTry';
            $_SESSION['currentTeacherId'] = 'noteacher';
            $flag                         = -1;
        }
        /* 操作未输入，之前输过，但这次换老师了 */
        elseif(!$Pcode && isset($_SESSION['code']) && $_SESSION['currentTeacherId'] != $teacher->teacher_id)
        {
            $_SESSION['code']             = 'fisrtTry';
            $_SESSION['currentTeacherId'] = 'noteacher';
            $flag                         = -1;
        }
        /* 操作未输入，但3小时内保留的SESSION正确，正好是当前老师 */
        elseif(!$Pcode && $_SESSION['code'] == $teacher->accesscode && $_SESSION['currentTeacherId'] == $teacher->teacher_id)
        {
            $flag = 1;
        }
        /* 操作已输入，且输入正确 */
        elseif(isset($Pcode) && $Pcode == $teacher->accesscode)
        {
            $_SESSION['code']             = $Pcode;
            $_SESSION['currentTeacherId'] = $teacher->teacher_id;
            $flag                         = 1;
        }
        /* 操作未输入，且保留的SESSION错误/操作已输入，但输入错误 */
        else
        {
            $_SESSION['code']             = 'noaccess';
            $_SESSION['currentTeacherId'] = 'noteacher';
            $flag                         = 0;
        }

        /**
         * 符合条件时取得tdocs
         * 教师课件表：一个课程可以有多个课件，一个老师可以有多个课程。
         * 根据 teacher_id, course_id 共同决定文件集合
         *
         * {condition : $flag == 1}
         * @var array $tdocs
         */
        $tdocs = NULL;
        if($flag == 1)
        {
            $tdocs = Tdoc::find('teacher_id = ? AND course_id = ?', $Gteacher_id, $Gcourse_id)->all()->query();
        }

        /**
         * CSS HIGHLIGHT
         *
         * @var string $css
         */
        $css = "#course-list a[href$='course_id/{$Gcourse_id}']".HIGHLIGHT_CSS;

        $this->_view['css']        = $css;
        $this->_view['teacher']    = $teacher;
        $this->_view['flag']       = $flag;
        $this->_view['tdocs']      = $tdocs;
        $this->_view['Gcourse_id'] = $Gcourse_id;
    }
}